package light.mvc.controller.patient;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.User;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.Status;
import light.mvc.page.patient.DoctorRegistration;
import light.mvc.service.admin.UserServiceI;
import light.mvc.service.patient.SiteRegisterServiceI;
import light.mvc.utils.TimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/siteRegister")
public class SiteRegisterController extends BaseController {

	@Autowired
	private SiteRegisterServiceI siteRegisterService;
	
	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("errorCode", "1");
			return "/register/siteRegister";
		}
		request.setAttribute("msg", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeSegment));
		request.setAttribute("errorCode", "0");
		return "/register/siteRegister";
	}
	
	@RequestMapping("/doctorDataGrid")
	@ResponseBody
	public Grid doctorDataGrid(DoctorRegistration doctorRegistration, PageFilter ph) {
		Grid grid = new Grid();

		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			e.printStackTrace();
			return grid;
		}
		
		List<DoctorRegistration> doctorRegistrations;
		doctorRegistrations = siteRegisterService.doctorGrid(doctorRegistration,timeSegment,ph);
		grid.setRows(doctorRegistrations);
		grid.setTotal(siteRegisterService.doctorGridCount(doctorRegistration, ph));
		return grid;
	}

	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request,Integer doctorId,String timeSegmentStr) {
		request.setAttribute("doctorId", doctorId);
		request.setAttribute("timeSegmentStr", timeSegmentStr);
		return "/register/siteRegisterAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Integer doctorId,String timeSegmentStr,String idNumber) {
		String errorMsg = null;
		int errorCode = -1;
		Json j = null;
		
		User patient = userService.getByIdNumber(idNumber);
		if(patient == null) {
			errorMsg = "该用户未注册";
			j = new Json(new Status(errorCode,errorMsg),null);
		} else {
			try {
				siteRegisterService.add(doctorId, patient.getId(), timeSegmentStr);
				errorCode = 0;
				errorMsg = "成功";
				j = new Json(new Status(errorCode,errorMsg),null);
			} catch(Exception e) {
				errorMsg = e.getMessage();
				j = new Json(new Status(errorCode, errorMsg),null);
			}
		}
		
		
		return j;

	}
}
